package com.xuan.system.service.sysUser.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuan.common.core.domain.LoginUser;
import com.xuan.common.core.domain.R;
import com.xuan.common.core.enums.ResultCode;
import com.xuan.common.core.enums.UserIdentity;
import com.xuan.common.security.service.TokenService;
import com.xuan.system.domain.sysUser.dto.LoginSysUserDTO;
import com.xuan.system.domain.sysUser.entity.SysUser;
import com.xuan.system.domain.sysUser.vo.SysUserVo;
import com.xuan.system.mapper.SysUserMapper;
import com.xuan.system.service.sysUser.SysUserService;
import com.xuan.system.utils.BCryptUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

/**
* @author 清凉茶
* @description 针对表【sys_user(管理员用户表)】的数据库操作Service实现
* @createDate 2024-08-05 21:49:10
*/
@Service
@RefreshScope
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
    implements SysUserService{

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private TokenService tokenService;

    @Value("${jwt.secret}")
    private String secret;

    @Override
    public R<String> login(LoginSysUserDTO loginSysUserDTO) {
        String userAccount = loginSysUserDTO.getUserAccount();
        String password = loginSysUserDTO.getPassword();
        if (StringUtils.isAnyBlank(userAccount, password)) {
            return R.fail("参数为空");
        }

        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        SysUser sysUser = sysUserMapper.selectOne(queryWrapper
                // 查询用户id和密码
                .select(SysUser::getUserId, SysUser::getNickName, SysUser::getPassword)
                // 根据账户名查询
                .eq(SysUser::getUserAccount, userAccount));
        // 用户不存在（账户名错误）
        if (sysUser == null) {
            return R.fail(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        // 密码错误
        boolean b = BCryptUtils.matchesPassword(password, sysUser.getPassword());
        if (!b) {
            return R.fail(ResultCode.FAILED_LOGIN);
        }

        // 保存用户的敏感信息到 redis(其中 key 应为每个用户的唯一标识)，生成 token
        // token 中多存储用户的唯一标识 key，作为登录是否过期的依据
        String token = tokenService.createToken(sysUser.getUserId(), secret, UserIdentity.ADMIN.getValue(), sysUser.getNickName(), null);
        return R.ok(token);
    }


    @Override
    public boolean logout(String token) {
        return tokenService.removeToken(token, secret);
    }

    @Override
    public SysUserVo getUserInfo(String token) {
        LoginUser loginUser = tokenService.getUserInfo(token, secret);
        SysUserVo userVo = new SysUserVo();
        userVo.setNickName(loginUser.getNickName());
        return userVo;

    }
}




